package cn.yunyichina.provider.pay.service.impl;

import cn.yunyichina.provider.framework.dubbo.service.AlipayService;
import cn.yunyichina.provider.iface.entity.alipay.AddAlipayCouponResponse;
import cn.yunyichina.provider.iface.entity.alipay.AddCouponRequest;
import cn.yunyichina.provider.iface.entity.alipay.AddCouponResponse;
import cn.yunyichina.provider.iface.entity.alipay.AlipayRequest;
import cn.yunyichina.provider.pay.constant.AppType;
import cn.yunyichina.provider.pay.exception.PaySettingNotFoundException;
import cn.yunyichina.provider.pay.service.BaseCouponService;
import cn.yunyichina.provider.pay.service.CouponService;
import cn.yunyichina.utils.convert.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 第三方卡券对接
 * Created by Lullaby on 2017/3/27.
 */
@Service
public class CouponServiceImpl extends BaseCouponService implements CouponService {

    private static final Logger logger = LoggerFactory.getLogger(CouponServiceImpl.class);

    /**
     * 卡券发放
     *
     * @param request
     * @return
     */
    public AddCouponResponse addCouponInstance(AddCouponRequest request) {
        logger.info("CouponServiceImpl.addCouponInstance.request -> {}", JsonUtils.toJsonString(request));

        try {
            String appType = request.getAppType();

            switch (AppType.valueOf(appType)) {
                case ALIPAY:
                    return addAlipayCouponInstance(request);
                case WECHAT:
                    return addWechatCouponInstance(request);
                default:
                    return null;
            }
        } catch (Exception e) {
            logger.error("CouponServiceImpl.addCouponInstance.Exception", e);
            return null;
        }
    }

    /**
     * 新增支付宝卡券实例
     *
     * @param request
     * @return
     */
    private AddAlipayCouponResponse addAlipayCouponInstance(AddCouponRequest request) throws PaySettingNotFoundException {
        AlipayRequest alipayRequest = buildAddAlipayCouponInstanceParameters(request);
        logger.info("CouponServiceImpl.addAlipayCouponInstance.alipayRequest -> {}", JsonUtils.toJsonString(alipayRequest));

        AddAlipayCouponResponse addAlipayCouponResponse = AlipayService.getAlipayClient().addAlipayCouponInstance(alipayRequest);
        logger.info("CouponServiceImpl.addAlipayCouponInstance.addAlipayCouponResponse -> {}", JsonUtils.toJsonString(addAlipayCouponResponse));

        return addAlipayCouponResponse;
    }

    /**
     * 新增微信卡券实例
     *
     * @param request
     * @return
     */
    private AddCouponResponse addWechatCouponInstance(AddCouponRequest request) {
        return null;
    }

}
